#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: backup.py
# Author: Tomás Vírseda
# License: GPL v3
# Description:  The backup manager perform export and import operations
import datetime

from vazaarlib.constants import BACKUP_DIR

class BackupManager():
    def __init__(self, gui):
        self.gui = gui
        self.store = self.gui.app.store

        # export related widget
        self.gui.frontend['cntexport'] = self.gui.glade.widgets.get_widget("cntExportOp")
        self.gui.frontend['backuptitle'] = self.gui.glade.widgets.get_widget("lblBackupOperation")
        self.gui.frontend['basicexport'] = self.gui.glade.widgets.get_widget("rbBasicExport")
        self.gui.frontend['fullexport'] = self.gui.glade.widgets.get_widget("rbFullExport")
        self.gui.frontend['dailybackup'] = self.gui.glade.widgets.get_widget("chkDailyBackup")
        self.gui.frontend['backupdir'] = self.gui.glade.widgets.get_widget("flcBackupDir")
        self.gui.frontend['execbackup'] = self.gui.glade.widgets.get_widget("btnExecuteBackup")
        self.gui.frontend['resexport'] = self.gui.glade.widgets.get_widget("lblResultBackup")
        # import related widgets
        self.gui.frontend['cntimport'] = self.gui.glade.widgets.get_widget("cntImportOp")
        self.gui.frontend['importfile'] = self.gui.glade.widgets.get_widget("flcImportFile")
        self.gui.frontend['execrestore'] = self.gui.glade.widgets.get_widget("btnExecuteRestore")
        self.gui.frontend['resimport'] = self.gui.glade.widgets.get_widget("lblResultRestore")


        self.gui.frontend['connections'] = {
            'btnExecuteBackup/clicked'  :   self.do_export,
            'btnExecuteRestore/clicked'  :   self.do_import,
            'chkDailyBackup/toggled'     :   self.daily_backup_toggled
        }

        #connect signals
        for wid_con, func in self.gui.frontend['connections'].iteritems():
            wid,con = wid_con.split('/')
            self.gui.glade[wid].connect(con,func)


    def show_export_operations(self):
        self.gui.frontend['cntexport'].show_all()
        self.gui.frontend['cntimport'].hide()
        self.gui.frontend['backuptitle'].set_markup("<big><b>Export Operations</b></big>")
        self.gui.frontend['basicexport'].set_active(True)
        self.gui.frontend['backupdir'].set_current_folder(BACKUP_DIR)

    def show_import_operations(self):
        self.gui.frontend['cntexport'].hide()
        self.gui.frontend['cntimport'].show_all()
        self.gui.frontend['backuptitle'].set_markup("<big><b>Import Operations</b></big>")
        self.gui.frontend['backupdir'].set_current_folder(BACKUP_DIR)

    def do_export(self, *args):
        folder = self.gui.frontend['backupdir'].get_current_folder()
        now = datetime.datetime.now()
        bdate = '%4d%02d%02d' % (now.year, now.month, now.day)
        btime = '%02d%02d%02d' % (now.hour, now.minute, now.second)
        filename = folder + '/backup-' + bdate + btime + '.rdf'
        result = self.store.do_backup(filename)
        if result:
            message = '<b>Backup done</b> '
        self.gui.frontend['resexport'].set_markup('<big>%s: <i>%s</i></big>' % (message, filename))

    def do_import(self, *args):
        filename = self.gui.frontend['importfile'].get_filename()
        result  = self.store.do_import(filename)
        self.gui.frontend['resimport'].set_markup('<big>%s</big>' % result)
        self.gui.actions.update_gui()

    def daily_backup_toggled(self, *args):
        toggled = self.gui.frontend['dailybackup'].get_active()

